//
//  UIAlertController+Blocks.m
//  Hestia
//
//  Created by liufengting on 2018/6/21.
//  Copyright © 2018年 com.hestia. All rights reserved.
//

#import "UIAlertController+Blocks.h"

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

@implementation UIAlertController (Blocks)

+ (UIAlertController *)alertWithTitle:(nullable NSString *)title message:(nullable NSString *)message buttonArray:(NSArray<NSString *> *)buttonArray completion:( void(^ _Nonnull )(NSInteger selectedIndex))completion {
    return [self alertControllerWithTitle:title
                                  message:message
                                    style:UIAlertControllerStyleAlert
                               showCancel:NO
                              buttonArray:buttonArray
                                   cancel:^{}
                               completion:completion];
}

+ (UIAlertController *)alertWithTitle:(nullable NSString *)title message:(nullable NSString *)message buttonArray:(NSArray<NSString *> *)buttonArray cancel:(void(^)(void))cancel completion:( void(^ _Nonnull )(NSInteger selectedIndex))completion {
    return [self alertControllerWithTitle:title
                                  message:message
                                    style:UIAlertControllerStyleAlert
                               showCancel:YES
                              buttonArray:buttonArray
                                   cancel:cancel
                               completion:completion];
}

+ (instancetype _Nonnull)alertWithTitle:(NSString *)title message:(NSString *)message textFeildPlaceholder:(NSString *)placeHolder buttonArray:(NSArray<NSString *> *)buttonArray cancel:(void (^)(void))cancel completion:(void (^)(NSInteger selectedIndex, NSString *text))completion {
    return [self alertControllerWithTitle:title
                                  message:message
                     textFeildPlaceholder:placeHolder
                               showCancel:YES
                              buttonArray:buttonArray
                                   cancel:cancel
                               completion:completion];
}

+ (UIAlertController *)actionSheetWithTitle:(nullable NSString *)title message:(nullable NSString *)message buttonArray:(NSArray<NSString *> *)buttonArray completion:( void(^ _Nonnull )(NSInteger selectedIndex))completion {
    return [self alertControllerWithTitle:title
                                  message:message
                                    style:UIAlertControllerStyleActionSheet
                               showCancel:NO
                              buttonArray:buttonArray
                                   cancel:^{}
                               completion:completion];
}

+ (UIAlertController *)actionSheetWithTitle:(nullable NSString *)title message:(nullable NSString *)message buttonArray:(NSArray<NSString *> *)buttonArray cancel:(void(^)(void))cancel completion:( void(^ _Nonnull )(NSInteger selectedIndex))completion {
    return [self alertControllerWithTitle:title
                                  message:message
                                    style:UIAlertControllerStyleActionSheet
                               showCancel:YES
                              buttonArray:buttonArray
                                   cancel:cancel
                               completion:completion];
}

+ (UIAlertController *)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message style:(UIAlertControllerStyle)style showCancel:(BOOL)showCancel buttonArray:(NSArray<NSString *> *)buttonArray cancel:(void(^)(void))cancel completion:( void(^ _Nonnull )(NSInteger selectedIndex))completion {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:message
                                                                      preferredStyle:style];
    for (NSInteger i = 0; i < buttonArray.count; i++) {
        UIAlertAction * action = [UIAlertAction actionWithTitle:buttonArray[i]
                                                          style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * _Nonnull action) {
                                                            if (completion) {
                                                                completion([buttonArray indexOfObject:action.title]);
                                                            }
                                                        }];
        [alertController addAction:action];
    }
    if (showCancel) {
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                                style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * _Nonnull action) {
                                                                  if (cancel) {
                                                                      cancel();
                                                                  }
                                                              }];
        [alertController addAction:cancelAction];
    }
    return alertController;
}

+ (UIAlertController *)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message textFeildPlaceholder:(NSString *)placeHolder showCancel:(BOOL)showCancel buttonArray:(NSArray<NSString *> *)buttonArray cancel:(void(^)(void))cancel completion:( void(^ _Nonnull )(NSInteger selectedIndex, NSString *text))completion {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:message
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = placeHolder;
    }];
    for (NSInteger i = 0; i < buttonArray.count; i++) {
        UIAlertAction * action = [UIAlertAction actionWithTitle:buttonArray[i]
                                                          style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * _Nonnull action) {
                                                            if (completion) {
                                                                completion([buttonArray indexOfObject:action.title], alertController.textFields.firstObject.text);
                                                            }
                                                        }];
        [alertController addAction:action];
    }
    if (showCancel) {
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                                style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * _Nonnull action) {
                                                                  if (cancel) {
                                                                      cancel();
                                                                  }
                                                              }];
        [alertController addAction:cancelAction];
    }
    return alertController;
}


@end

@implementation UIViewController (AlertBlocks)

- (void )alertWithTitle:(nullable NSString *)title message:(nullable NSString *)message buttonArray:(NSArray<NSString *> *)buttonArray completion:( void(^ _Nonnull )(NSInteger selectedIndex))completion {
    [self alertControllerWithTitle:title
                           message:message
                             style:UIAlertControllerStyleAlert
                        showCancel:NO
                       buttonArray:buttonArray
                        sourceView:nil
                            cancel:^{}
                        completion:completion];
}

- (void )alertWithTitle:(nullable NSString *)title message:(nullable NSString *)message buttonArray:(NSArray<NSString *> *)buttonArray cancel:(void(^)(void))cancel completion:( void(^ _Nonnull )(NSInteger selectedIndex))completion {
    [self alertControllerWithTitle:title
                           message:message
                             style:UIAlertControllerStyleAlert
                        showCancel:YES
                       buttonArray:buttonArray
                        sourceView:nil
                            cancel:cancel
                        completion:completion];
}

- (void)alertWithTitle:(nullable NSString *)title
               message:(nullable NSString *)message
  textFeildPlaceholder:(nullable NSString *)placeHolder
           buttonArray:(NSArray<NSString *> * _Nonnull )buttonArray
                cancel:(void(^ _Nonnull )(void))cancel
            completion:( void(^ _Nonnull )(NSInteger selectedIndex, NSString *text))completion {
    UIAlertController *alertController = [UIAlertController alertWithTitle:title
                                                                   message:message
                                                      textFeildPlaceholder:placeHolder
                                                               buttonArray:buttonArray
                                                                    cancel:cancel
                                                                completion:completion];
    [self presentViewController:alertController animated:YES completion:NULL];
}

- (void )actionSheetWithTitle:(nullable NSString *)title message:(nullable NSString *)message buttonArray:(NSArray<NSString *> *)buttonArray sourceView:(UIView * _Nullable)sourceView completion:(void (^ _Nonnull)(NSInteger))completion {
    return [self alertControllerWithTitle:title
                                  message:message
                                    style:UIAlertControllerStyleActionSheet
                               showCancel:NO
                              buttonArray:buttonArray
                               sourceView:sourceView
                                   cancel:^{}
                               completion:completion];
}

- (void )actionSheetWithTitle:(nullable NSString *)title message:(nullable NSString *)message buttonArray:(NSArray<NSString *> *)buttonArray sourceView:(UIView * _Nullable)sourceView cancel:(void(^)(void))cancel completion:( void(^ _Nonnull )(NSInteger selectedIndex))completion {
    return [self alertControllerWithTitle:title
                                  message:message
                                    style:UIAlertControllerStyleActionSheet
                               showCancel:YES
                              buttonArray:buttonArray
                               sourceView:sourceView
                                   cancel:cancel
                               completion:completion];
}

- (void )alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message style:(UIAlertControllerStyle)style showCancel:(BOOL)showCancel buttonArray:(NSArray<NSString *> *)buttonArray sourceView:(UIView * _Nullable)sourceView cancel:(void(^)(void))cancel completion:( void(^ _Nonnull )(NSInteger selectedIndex))completion {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:message
                                                                               style:style
                                                                          showCancel:showCancel
                                                                         buttonArray:buttonArray
                                                                              cancel:cancel
                                                                          completion:completion];
    if (alertController.preferredStyle == UIAlertControllerStyleActionSheet && IS_IPAD) {
        UIPopoverPresentationController *popPresenter = [alertController popoverPresentationController];
        popPresenter.sourceView = sourceView;
        popPresenter.sourceRect = sourceView.bounds;
        [self presentViewController:alertController animated:YES completion:NULL];
    } else {
        [self presentViewController:alertController animated:YES completion:NULL];
    }
}

@end

